<template>
  <div>
    <!-- 新增 修改 用户-->
    <el-dialog
      width="500px"
      title="title"
      :title="type == 'add' ? $t('添加') : $t('编辑')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="type == 'add'">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="form.phonenumber" />
        </el-form-item>
        <el-form-item label="性别">
          <!-- sex	性别 0:男 1:女 2:未知 -->
          <el-radio-group v-model="form.sex">
            <el-radio :label="'0'">男</el-radio>
            <el-radio :label="'1'">女</el-radio>
            <el-radio :label="'2'">未知</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="权限*" prop="targetRoleId">
          <el-select
            style="width: 100%"
            v-model="form.targetRoleId"
            clearable
            filterable
          >
            <el-option
              v-for="item in sysRoleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSysRoleList,
  getUserInfo,
  insertUserByPlatform,
  updateUserByPlatform,
} from "@/api/platform/userManage";

export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: {},
    },
    /**
     * type
     */
    type: {
      type: String,
      default: "add",
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      /**角色权限列表 */
      sysRoleList: [],
      form: {},
      // 表单校验
      rules: {
        nickName: [{ required: true, message: "请输入", trigger: "blur" }],
        email: [
          {
            type: "email",
            required: true,
            message: "请输入正确的邮箱地址",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入", trigger: "blur" }],
        targetRoleId: [{ required: true, message: "请选择", trigger: "blur" }],
      },
    };
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.resetForm();
        this.getSysRoleList();
        if (this.type == "edit") {
          this.getUserInfo();
        }
      }
    },
  },
  methods: {
    getUserInfo() {
      getUserInfo(this.formData.userId).then((res) => {
        const { data } = res;
        Object.keys(data).forEach((key) => {
          // this.form[key] = data[key];
          this.$set(this.form, key, data[key]);
        });
        this.$set(this.form, "targetRoleId", data["roleId"]);

        console.log(`this.form ==>`, JSON.parse(JSON.stringify(this.form)));
      });
    },
    getSysRoleList() {
      getSysRoleList(this.formData.distrType || "sys_user").then((res) => {
        const { code, data, msg } = res;
        if (code === 200) {
          this.sysRoleList = data;
        }
      });
    },
    handleSubmit() {
      this.$refs["formRef"].validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.form));
          if (this.type == "add") {
            data.roleId = data.targetRoleId;
            insertUserByPlatform(data).then((res) => {
              const { code, msg } = res;
              if (code === 200) {
                this.$message({ type: 'success', message: msg})
;
                this.cancel();
                this.$emit("getList");
              }
            });
          } else {
            updateUserByPlatform(data).then((res) => {
              const { code, msg } = res;
              if (code === 200) {
                this.$message({ type: 'success', message: msg})
;
                this.cancel();
                this.$emit("getList");
              }
            });
          }
        }
      });
    },
    resetForm() {
      Object.keys(this.form).forEach((key) => (this.form[key] = ""));
    },

    cancel() {
      this.$emit("update:show", false);
    },
  },
};
</script>
<style scoped lang="scss"></style>
